#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sets import Set
from random import randint
import itertools

def randrange(a,b):
    return randint(a,b)

#random test for primality from Solovay and Strassen
def solovay_strassen(n, k=10):
    if n == 2:
        return True
    if not n & 1:
        return False
 
    def legendre(a, p):
        if p < 2:
            raise ValueError('p must not be < 2')
        if (a == 0) or (a == 1):
            return a
        if a % 2 == 0:
            r = legendre(a / 2, p)
            if p * p - 1 & 8 != 0:
                r *= -1
        else:
            r = legendre(p % a, a)
            if (a - 1) * (p - 1) & 4 != 0:
                r *= -1
        return r
 
    for i in xrange(k):
        a = randrange(2, n - 1)
        x = legendre(a, n)
        y = pow(a, (n - 1) / 2, n)
        if (x == 0) or (y != x % n):
            return False
 
    return True


def primes_sieve(limit):
    limitn = limit+1
    not_prime = [False] * limitn
    primes = []

    for i in range(2, limitn):
        if not_prime[i]:
            continue
        for f in xrange(i*2, limitn, i):
            not_prime[f] = True

        primes.append(i)

    return primes

def checkMaxPrimeRange(a,b):
    maxN=0
    try:
        primeFound=False
        n=0        
        while not primeFound:
            quadExpr=n*n+a*n+b
            if solovay_strassen(quadExpr):
                maxN=n            
            else: 
                primeFound=True
            n +=1
    except:
        #ignore
        print "Exception for a=",a, "b=",b
    return maxN

def checkMaxabPrimeRange(amax,bmax):
    maxPrimCycle=0
    opta=-10000
    optb=-10000
    for a in range(-amax,amax):
        for b in range(-bmax,bmax):
          cycle=checkMaxPrimeRange(a,b)
          if cycle>maxPrimCycle:
              maxPrimCycle=cycle
              opta=a 
              optb=b 
    return [maxPrimCycle,opta, optb]           


#print checkMaxabPrimeRange(1000,1000)

print checkMaxPrimeRange(-61,971)

print -61*971